package com.songtech.uitest.module.archive;

import com.songtech.uitest.components.IBrowserDriverFactory;
import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.AlertHandler;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.module.order.OrderTower;
import com.songtech.uitest.module.screen.ScreenTower;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.resources.ServerResource;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class ArchiveTower {

    @Autowired
    @Qualifier("browserDriver")
    private IBrowserDriverFactory driverCreater;

    @Autowired
    @Qualifier("indentResource")
    private IndentResource indentResource;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private AlertHandler alertHandler;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private StepParser stepParser;

    private Logger logger = LoggerFactory.getLogger(OrderTower.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;

    @Autowired
    private ServerResource serverResource;


    /**
     * 模拟归档操作。目前写死用辅浏览器进行直接访问操作
     * @param step 没有使用。但调用时个位要写>0且！=9的正整数。以便收尾时恢复到辅账号的初始化状态。比如写01
     * @throws Exception
     */
    public void archiveTower(String step) throws Exception {

        WebDriver driver = driverCreater.getViceDriver();
        driver.get(serverResource.getWsArchivePath());
        Thread.sleep(5000);

        logger.info(space.getSpace() + "开始填写归档详情表单");

        //填表单
        //服务开始时间
        driver.findElement(By.id("datetimepicker1")).findElement(By.cssSelector("span.glyphicon.glyphicon-calendar")).click();
        //服务结束时间
        driver.findElement(By.id("datetimepicker2")).findElement(By.cssSelector("span.glyphicon.glyphicon-calendar")).click();

        driver.findElement(By.id("towerOrderCode")).sendKeys(indent.getTowerId());

        driver.findElement(By.id("province")).clear();
        driver.findElement(By.id("province")).sendKeys(indentResource.getArchiveProvince());
        driver.findElement(By.id("region")).clear();
        driver.findElement(By.id("region")).sendKeys(indentResource.getArchiveRegion());
        driver.findElement(By.id("county")).clear();
        driver.findElement(By.id("county")).sendKeys(indentResource.getArchiveCountry());

        //运营商地市
        driver.findElement(By.id("regionId")).clear();
        driver.findElement(By.id("regionId")).sendKeys(indentResource.getArchiveRegionId());
        //需求承接地市
        driver.findElement(By.id("regionCode")).clear();
        driver.findElement(By.id("regionCode")).sendKeys(indentResource.getArchiveRegionCode());
        //站址所属地市
        driver.findElement(By.id("siteRegion")).clear();
        driver.findElement(By.id("siteRegion")).sendKeys(indentResource.getArchiveSiteRegion());

        //站址名称
        driver.findElement(By.id("siteName")).clear();
        driver.findElement(By.id("siteName")).sendKeys(indentResource.getArchiveSiteName());


        driver.findElement(By.id("saveSet")).click();


        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        logger.info(space.getSpace() + "表单归档提交完成");

        Thread.sleep(6000);

        //关闭弹出框
        Alert alert = driver.switchTo().alert();
        logger.info(space.getSpace() + "弹出提示框：" + alert.getText());
        alert.accept();


    }


    public void archive3OneTower(String step) throws Exception {

        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //打开归档列表页
        menuHandler.clickMenuItem(driver, "铁塔建设管理", "需求归档", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);


        //查询此单
        driver.findElement(By.id("keywords")).sendKeys(indent.getMobileId());
        driver.findElement(By.xpath("//button[text()='查询']")).click();
        Thread.sleep(6000);
        //选择最新的一条进行提交
        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input")).click();
        driver.findElement(By.xpath("//button[text()='归档']")).click();

        Thread.sleep(5000);

        alertHandler.checkAndGo(driver);

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        logger.info(space.getSpace() + "三方归档提交完成。");

        tabHandler.closeAllTabs(driver);

    }

}
